
function createXMLDocByAjax(r) //创建XMLDOM对象函数，跨浏览器解决方案
{
    var xmlDom;
    xmldata = r.responseText;
    if (window.ActiveXObject) //IE
    {
        xmlDom = new ActiveXObject("Microsoft.XMLDOM");
        xmlDom.async = false;
        xmlDom.loadXML(xmldata);
        if (xmlDom.parseError.errorCode != 0)
        {
            window.alert("XML文件加载错误. errorCode：" + xmlDom.parseError.errorCode + " reason：" + xmlDom.parseError.reason);
            return false;
        }
        else
        {return xmlDom;}
    }
    else if (document.implementation && document.implementation.createDocument) //FireFox
    {
        xmlDom = document.implementation.createDocument("","",null);
        xmlDom.async = false;
        try {
            xmlDom.loadXML(xmlString);
        } catch (e) {
            xmlDom = (new DOMParser()).parseFromString(xmlString, "text/xml");
        }
        return xmlDom;
    }
    else
    {
        window.alert("不支持XMLDOM对象");
        return false;
    }
}
function treeMenu(ulID,fCmd,Query)
{
    var xmlDom = createXMLDom(xmlfile); //创建XMLDOM对象
	var q = 'f='+encodeURI(fCmd)+'&'+sQuery;
	new Ajax.Request('/xmlhttp.html',{asynchronous:false,parameters: q ,method: 'get',onComplete:createXMLDocByAjax.bind(this)});
    var menu = xmlDom.documentElement; //xml文档根节点
    var rootLevels = menu.getElementsByTagName("rootLevel"); //一级分类集合
        
    var ul = $(ulID);
    for (var i=0;i<rootLevels.length;i++) //循环显示一级分类
    {
        var selfID = rootLevels[i].getAttribute("id");
        var selfName = rootLevels[i].getElementsByTagName("selfName")[0].firstChild.nodeValue;
        var li = document.createElement("li");
        li.innerHTML = selfName;
        li.id = selfID;
            
        var levels = rootLevels[i].getElementsByTagName("level"); //集合
        if (levels.length > 0) //有子类
        {
            li.className = "plus";
            li.onclick = function () {liClick(this,this.id,xmlDom);} //单击事件
        }
        else //无子类
        {
            li.className = "item";
        }
        ul.appendChild(li);
    }
}

function liClick(li,id,xmlDom,event)
{
    var childUL = li.getElementsByTagName("ul"); //ul集合
    
    if (childUL.length > 0)//折叠
    {closeChild(li,childUL);}
    else //展开
    {openChild(li,id,xmlDom);}
    
    cancelBuble(event); //plus停止事件冒泡
}

function openChild(li,id,xmlDom) //展开函数
{
    li.className = "minus"; //折叠图标
    
    var levels = xmlDom.documentElement.getElementsByTagName("level"); //level集合
    var ul = document.createElement("ul");
    for (var m=0;m<levels.length;m++)
    {
        var selfID = levels[m].getAttribute("id");
        var selfName = levels[m].getElementsByTagName("selfName")[0].firstChild.nodeValue;
        var parentID = levels[m].getElementsByTagName("parentID")[0].firstChild.nodeValue;
        
        if (id == parentID) //属于id子类
        {
            var ulli = document.createElement("li");
            ulli.innerHTML = selfName;
            ulli.id = selfID;
            
            var childLevels = levels[m].getElementsByTagName("level"); //集合
            if (childLevels.length > 0) //有子类
            {
                ulli.className = "plus";
                
                /*停止事件冒泡，跨浏览器解决方案 Start*/
                if (window.ActiveXObject) //IE
                {ulli.onclick = function () {liClick(this,this.id,xmlDom,event);}} //单击事件
                else //FireFox
                {ulli.onclick = function (event) {liClick(this,this.id,xmlDom,event);}} //单击
                /*停止事件冒泡，跨浏览器解决方案 End*/
            }
            else //无子类
            {
                ulli.className = "item";
                
                /*停止事件冒泡，跨浏览器解决方案 Start*/
                if (window.ActiveXObject) //IE
                {ulli.onclick = function () {cancelBuble(event);}} //item停止事件冒泡
                else //FireFox
                {ulli.onclick = function (event) {cancelBuble(event);}} //item停止事件冒泡
                /*停止事件冒泡，跨浏览器解决方案 End*/
            }
            
            ul.appendChild(ulli);
        }
        
    }
    
    li.appendChild(ul);
}

function closeChild(li,childUL) //折叠函数
{
    li.className = "plus"; //展开图标
    
    for (var j=0;j<childUL.length;j++) //循环删除ul,ul可能有子ul、li孙ul、li
    {childUL[j].parentNode.removeChild(childUL[j]);}
}

function cancelBuble(domEvent) //停止事件冒泡函数,跨浏览器解决方案
{
    if (window.ActiveXObject) //IE
    {domEvent.cancelBubble = true;}
    else //FireFox
    {domEvent.stopPropagation();}
}
